package org.cqust.backend.repository.mission;

import org.cqust.backend.entity.mission.MissionStepVersion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MissionStepVersionRepository extends JpaRepository<MissionStepVersion, Long> {

    // 寻找（归档或为归档的）StepVersion，通过missionId来寻找。
    @Query("SELECT msv FROM MissionStepVersion msv WHERE msv.missionId = :missionId and msv.obsolete = :obsolete order by msv.order")
    List<MissionStepVersion> findByMissionId(@Param("missionId") Long missionId, @Param("obsolete") Boolean obsolete);

    // 寻找已经归档的StepVersion。通过MissionVersionId来寻找
    @Query("SELECT msv FROM MissionStepVersion msv WHERE msv.missionVersionId = :missionVersionId and msv.obsolete = true")
    List<MissionStepVersion> findObsoletedByMissionVersionId(@Param("missionVersionId") Long missionVersionId);

    // 将某个missionId下的所有stepVersion归档到missionVersionId下
    @Query("UPDATE MissionStepVersion msv SET msv.missionVersionId = :missionVersionId, msv.obsolete = true WHERE msv.id = :stepId")
    void updateByStepVersionId_SetMissionVersionIdAndObsoleteTrue(@Param("stepId") Long stepId, @Param("missionVersionId") Long missionVersionId);
    @Modifying
    @Query("UPDATE MissionStepVersion msv set msv.configXml = :missionConfigXml where msv.id = :stepId")
    void updateXml(@Param("missionConfigXml") String missionConfigXml,@Param("stepId") Long stepId);
}
